<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\admin\model;

use app\admin\model\Model;
use Mscore\Core\Redis;
use Mscore\Core\Lib\File;

class Index extends Model
{

    private $logtable='user_log';
    private $onlinetable='user_online';
    private $account='user_account';
    private $role='sys_role';

    /**
     * 首页默认控制台
     * @param array $console 
     * @param array $where 
    */
    public function console(){
        $sys_info ['os'] = PHP_OS;
	      $sys_info ['NAME_server'] = $_SERVER ['SERVER_NAME'];
	      $sys_info ['PORT_server'] = $_SERVER ['SERVER_PORT'];
	      $sys_info ['IP_server'] = $_SERVER["SERVER_ADDR"];
        $systemver=config("app.systemver");
	      $sys_info ['vern'] = $systemver['version'];
        $sys_info ['vernumber']  = $systemver['vernumber'];
        $sys_info ['user'] = Model::name('user_cert')->where('status','>',2)->count('uid');
        $sys_info ['withdraw'] = Model::name('user_withdraw')->where('status',2)->sum('money');
        $sys_info ['recharge'] = Model::name('user_recharge')->where('status','>',2)->sum('money');
        return $sys_info;
    }

    /**
     * 登录记录
     * @param array $recordlogin 
     * @param array $where 
     */ 
    public function recordlogin($where=null,$page=1,$page_size=10){
        $recordlist['datalist'] = Model::name($this->logtable)
            ->field('id,aid, username, log_time, log_info, ip_address, roletype, equipment')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('aid desc, log_time desc')
            ->select()
            ->toArray();
            // print_r($recordlist);die;
        foreach ($recordlist['datalist'] as $key => $value) {
            $recordlist['datalist'][$key]['roletype']=Model::name($this->role)->where(['id'=>$value['roletype']])->value('title');
            //获取设备型号返回类型 1 iphone，2 ipad，3android，4windows nt，5mac os，6 other
            switch ($value['equipment']) {
                case 1:
                    $recordlist['datalist'][$key]['equipment']='iphone';
                    break;
                case 2:
                    $recordlist['datalist'][$key]['equipment']='ipad';
                    break;
                case 3:
                    $recordlist['datalist'][$key]['equipment']='android';
                    break;
                case 4:
                    $recordlist['datalist'][$key]['equipment']='windows nt';
                    break;
                case 5:
                    $recordlist['datalist'][$key]['equipment']='mac os';
                    break;
                default:
                    $recordlist['datalist'][$key]['equipment']='other';
                    break;
            }
        }
        //数据总数
        $recordlist['total']=Model::name($this->logtable)
            ->where($where)
            ->count('id');
        return $recordlist;    
    }

    /**
     * 在线人数
     * @param array $userlist 
     * @param array $where 
     */ 
    public function useronline($where=null,$page=1,$page_size=10){
        //部门数据
        $userlist['datalist'] = Model::name($this->onlinetable)
            ->alias('a')
            ->join($this->role.' b', 'a.roletype=b.id', 'left')
            ->field('a.id, a.ip,a.username,a.realname,a.mobile,a.time,a.aid, b.title as roletype')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id asc')
            ->select()
            ->toArray();
        //数据总数
        $userlist['total']=Model::name($this->onlinetable)
            ->where($where)
            ->count('id');
        return $userlist;   
    }

    /**
     * 在线人数图标接口
     * @param array $online 
     * @param array $where 
     */ 
    public function online(){
        $time = getTime(8,7);
        foreach ($time as $key => $value) {
            $user[$key] = Model::name($this->logtable)
                ->where('DATE_FORMAT(log_time,"%Y-%m-%d")="'.$value.'"')
                ->count('id');
        }
        return ['time'=>array_values($time),'user'=>array_values($user)];  
    }

    /**
     * 用户设备分布
     * @param array $ondevice 
     * @param array $where 
     */ 
    public function ondevice(){
        $device =['iphone','ipad','android','windows nt','mac os','other'];
        foreach ($device as $key => $value) {
            $data[$key]['value'] = Model::name($this->logtable)
                ->where('equipment='.($key+1))
                ->count('id');
            $data[$key]['name'] = $value;
        }
        return ['device'=>array_values($device),'data'=>array_values($data)];  
    }

    /**
     * 一周新增用户数
     * @param array $ondevice 
     * @param array $where 
     */ 
    public function newuser(){
        $time = getTime(8,7);
        foreach ($time as $key => $value) {
            $user[$key] = Model::name($this->account)
                ->where('DATE_FORMAT(createtime,"%Y-%m-%d")="'.$value.'"')
                ->count('aid');
        }
        return ['time'=>array_values($time),'user'=>array_values($user)]; 
    }

    /**
     * 获取缓存
     * @param array $cache 
     * @param array $where 
    */
    public function cache(){
        //缓存文件
      $tempPath = config("app.runtime_path");
	    $compilesize =(new File)->setupSize((new File)->dirSize($tempPath.'/compile/'));
      $cachesize =(new File)->setupSize((new File)->dirSize($tempPath.'/cache/'));
      $filepath=[
          "tempPath" =>$tempPath,
          'compilesize'=>$compilesize,
          "cachesize" => $cachesize,
      ];
      return $filepath;
    }

    /**
     * 删除缓存
     * @param array $cachedel 
     * @param array $where 
    */
	public function cachedel($file=null){
       //缓存文件
      $tempPath = config("app.runtime_path");
      switch (true) {
        case $file:
          $tempPath = $tempPath.'/'.$file.'/';
          break;
        default:
          $tempPath = $tempPath;
          break;
      }  
	    $delfile=(new File)->deleteTree($tempPath);
      return $delfile;
    }

    /**
     * 强制下线
     * @param array $offline 
     * @param array $where 
     */ 
   public function offline($id){ 
        if($id){
            $u=Model::name($this->onlinetable)
                ->where('id='.$id)
                ->value('aid,roletype,equipment');
            if(config("redis.redis")){
                Redis::getInstance()->runCommand('delete',[$u['roletype'].'.'.$u['aid'].'.'.$u['equipment']]); //删除token
                Redis::getInstance()->runCommand('delete',[$u['aid']]); //删除用户信息
            }
            Model::name($this->onlinetable)
                ->where('id='.$id)
                ->delete();
            return true;      
        }
        return false; 
    }

}